package com.zhao.controller;

import com.zhao.pojo.Student;
import com.zhao.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class WalletController {

    @Autowired
    private WalletService walletService;

    @ResponseBody
    @RequestMapping("student-yesReceiveEbick.do")
    public String studentYesReceiveEbick(String orderFormID,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "请先登录";
        }
        String res = walletService.studentYesReceiveEbick(student,orderFormID,session);
        System.out.println("res-studentYesReceiveEbick: "+res);

        return res;
    }

    @ResponseBody
    @RequestMapping("student-backMoney.do")
    public String studentBackMoney(String orderFormID,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "请先登录";
        }
        String res = walletService.studentBackMoney(student,orderFormID,session);
        System.out.println("res-studentBackMoney: "+res);

        return res;
    }

    @ResponseBody
    @RequestMapping("student-invest.do")
    public String studentInvest(String amount,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "请先登录";
        }
        ////////////直接充值，不做真实充值，此为项目边界
        String res = walletService.studentInvest(student,amount,session);
        System.out.println("res-studentInvest: "+res);
        return res;
    }

    @ResponseBody
    @RequestMapping("student-pay.do")
    public String studentPay(String orderFormID,String payPassword,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "请先登录";
        }
        System.out.println(orderFormID);
        System.out.println(payPassword);
        String res = walletService.studentPay(student,orderFormID,payPassword,session);
        System.out.println("res-studentPay: "+res);

        return res;
    }

    @ResponseBody
    @RequestMapping("student-ifhaveWallet.do")
    public String studentifhaveWallet(HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "请先登录";
        }

        String res = walletService.getMyWallet(student,session);
        System.out.println("res-ifhaveWallet: "+res);

        if(res.equals("success")){
            return "yes";
        }

        return "no";
    }


    @RequestMapping("student-getMyWallet.do")
    public String studentGetMyWallet(HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "redirect:/";
        }

        String res = walletService.getMyWallet(student,session);
        System.out.println("res-getMyWallet: "+res);

        if(res.equals("没有钱包")){
            ///注册钱包去
            System.out.println("tu creat one wallet page");
            return "redirect:/student-creatOneWallet";
        }

        return "redirect:/student-myWallet";
    }

    @ResponseBody
    @RequestMapping("student-TocreatOneWallet.do")
    public String studentCreatOneWallet(String password,String correctPassword, HttpSession session){
        //System.out.println(password);
        //System.out.println(correctPassword);
        Student student = (Student)session.getAttribute("student");
        if(student==null){
            return "请先登录";
        }
        if(!password.equals(correctPassword)){
            return "两次输入的密码不同";
        }
        String res = walletService.studentCreatOneWallet(student,password,session);
        System.out.println("res-studentCreatOneWallet: "+res);

        return res;
    }

}
